package com.example.administrator.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/3/17 0017.
 */
public class MyAdapterPractice extends BaseAdapter{
    private LayoutInflater mInflater;
    private List<News> mList;
    public MyAdapterPractice(Context context,List<News> list){
        mList = list;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.item_list2,null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
            viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.context = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        News news = mList.get(position);
        //动态赋值
        viewHolder.imageView.setImageResource(news.getPicId());
        viewHolder.title.setText(news.getTitle());
        viewHolder.context.setText(news.getSummary());
        return convertView;
    }
    class ViewHolder{
        public ImageView imageView;
        public TextView title;
        public TextView context;
    }
}
